﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class User_ChoosePaper : System.Web.UI.Page
{
    //Datacon dataconn = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //dataconn.ecDropDownList(ddlLesson, "select  * from Course", "c_name", "c_id");
            //dataconn.ecDropDownList(ddlQueName, "select * from TaoTi where c_id='" + ddlLesson.SelectedValue + "' and PaperState='"+1+"'", "PaperName", "PaperID");
            using (DataClassesDataContext ds = new DataClassesDataContext())
            {
                ddlLesson.DataSource = from q in ds.Course select q;
                ddlLesson.DataTextField = "c_name";
                ddlLesson.DataValueField = "c_id";
                ddlLesson.DataBind();

                ddlQueName.DataSource = from q in ds.TaoTi
                                        where q.c_id ==Convert.ToInt32(ddlLesson.SelectedValue)
                                        where q.PaperState == "1"
                                        select new {q.ID,q.PaperName };
                ddlQueName.DataTextField = "PaperName";
                ddlQueName.DataValueField = "ID";
                ddlQueName.DataBind();

            }
        }

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        //SqlConnection con = dataconn.getcon();
        //con.Open();
        //string sqlstr1 = "select count(*) from Score where UserID ='" + Session["ID"] + "' and PaperID='" + ddlQueName.SelectedValue + "'";
        //SqlCommand mycom = new SqlCommand(sqlstr1, con);
        //int intcount = Convert.ToInt32(mycom.ExecuteScalar());
        //Application["d1"] = ddlLesson.SelectedItem;
        //Session["PaperID"] = ddlQueName.SelectedValue;
        //Session["PaperName"] = ddlQueName.SelectedItem;
        //if (this.ddlQueName.Text == "")
        //{
        //    Response.Write("<script lanuage=javascript>alert('您还没有选择考试套题，请重新选择！');location='javascript:history.go(-1)'</script>");
        //}
        //else if (intcount > 0)
        //{
        //    Response.Write("<script lanuage=javascript>alert('此套题已经答过！');location='javascript:history.go(-1)'</script>");
        //}
        //else
        //{
        //    Response.Redirect("wait.aspx");
        //}
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            bool have = (from q in ds.Score
                         where q.UserID == Convert.ToInt32(Session["ID"])
                         where q.PaperID == Convert.ToInt32(ddlQueName.SelectedValue)
                         select q).Any();
            Application["d1"] = ddlLesson.SelectedItem;
            Session["PaperID"] = ddlQueName.SelectedValue;
            Session["PaperName"] = ddlQueName.SelectedItem;
            if (this.ddlQueName.Text == "")
            {
                Response.Write("<script lanuage=javascript>alert('您还没有选择考试套题，请重新选择！');location='javascript:history.go(-1)'</script>");
            }
            else if (have)
            {
                Response.Write("<script lanuage=javascript>alert('此套题已经答过！');location='javascript:history.go(-1)'</script>");
            }
            else
            {
                Response.Redirect("wait.aspx");
            }
        }
    }


    protected void ddlLesson_SelectedIndexChanged(object sender, EventArgs e)
    {
        //dataconn.ecDropDownList(ddlQueName, "select * from TaoTi where c_id='" + ddlLesson.SelectedValue + "' and PaperState='" + 1 + "'", "PaperName", "PaperID");
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            ddlQueName.DataSource = from q in ds.TaoTi
                                    where q.c_id == Convert.ToInt32(ddlLesson.SelectedValue)
                                    where q.PaperState == "1"
                                    select q;
            ddlQueName.DataBind();
            ddlQueName.DataTextField = "PaperName";
            ddlQueName.DataValueField = "PaperID";
        }
    }
    protected void ddlQueName_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}